var jobReference = {
	currentJobReferencePageNo:0,
	
	create : function(createMode) {
		var subject = $('#subject').val();
		var lightSoure = $('#lightSoure').val();
		var jobReferenceFilePath = $('#jobReferenceFilePath').val();
		
        if(!subject){
           $.msgBox.alert({
                title : 'Tips',
                msg : 'Subject is null,please input a value!',
                icon : 'warning',
                width : 350
            });
            return false;
        }
        if(!lightSoure){
           $.msgBox.alert({
                title : 'Tips',
                msg : 'Light soure is null,please input a value!',
                icon : 'warning',
                width : 350
            });
            return false;
        }
        if(!jobReferenceFilePath){
           $.msgBox.alert({
                title : 'Tips',
                msg : 'Preview image is null,please select a image file!',
                icon : 'warning',
                width : 450
            });
            return false;
        }
		
		$.ajax({
			type : "POST",
			url : "../jobReference/"+createMode+".json",
			cache : false,
			dataType : "json",
			data : {
				"id" : $('#jobReferenceId').val(),
				"subject" : subject,
				"lightSoure" : lightSoure,
				"imagePath" : jobReferenceFilePath
			},
			success : function(data) {
				if (data.result == 'success') {
					$.msgBox.alert({
						title : 'Success',
						msg : 'Operate Success!',
						icon : 'warning',
						width : 250,
						yesFun : function() {
							$('#jobReferenceMenu').click();
						}
					});
				} else {
					$.msgBox.alert({
						title : 'Tips',
						msg : 'System error!Please try again later!',
						icon : 'warning',
						width : 350
					});
				}
			}
		});
	},
    list:function(page){
        $.ajax({
            type: "GET",
            url: "../jobReference/list.json",
            cache:false,
            dataType: "json",
            data : {"currentPage":page + 1,"perPageSize":10},
            success: function(data){
                if(data.result == 'success'){
                	global.jobReferenceMap = new HashMap();
                	
                    $('#jobReferenceList').html("");

                    $.each(data.jobReferenceList, function(index, obj) {
                    	global.jobReferenceMap.put(obj.id,obj);
                    	
                        var html = "<tr><td>"+obj.subject+"</td><td>"+obj.lightSoure+"</td><td>"+new Date(obj.createDate).pattern("yyyy-MM-dd HH:mm:ss")+"</td>"+
                            "<td><a href='javascript:jobReference.top("+obj.id+");'>Top</a> | " +
                            "<a href='javascript:jobReference.toModifyJobReferencePage("+obj.id+");'>Modify</a> | " +
                            "<a href='javascript:jobReference.deleteJobReference("+obj.id+");'>Delete</a></td></tr>";
                            
                        $('#jobReferenceList').append(html);
                    });
                    
                    $("#paginationDiv").pagination(data.jobReferenceListCount, {
                        callback: jobReference.list,//回调函数
                        num_display_entries: 5,
                        num_edge_entries: 3,
                        items_per_page:10,//每页显示多少条
                        current_page:page//当前页码
                    });
                    
                    currentJobReferencePageNo = page;
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    },
    deleteJobReference:function(id){
        $.msgBox.prompt({
            title:'Tips',
            msg:'Are you sure delete this record?',
            icon:'warning',
            width:350,
            yesFun:function(){
                $.ajax({
                    type: "POST",
                    url: "../jobReference/delete.json",
                    cache:false,
                    dataType: "json",
                    data : {"id":id},
                    success: function(data){
                        if(data.result == "success"){
                            jobReference.list(currentJobReferencePageNo);
                        }else{
                            $.msgBox.alert({
                                title:'Tips',
                                msg:'System error!Please try again later!',
                                icon:'warning',
                                width:350
                            });
                        }
                    }
               });
            }
        });
    },
    toModifyJobReferencePage:function(jobReferenceId){
        $('#loadCnt').load("jobReference/jobReference.jsp?jobReferenceId="+jobReferenceId);
    },
    modify:function(jobReferenceId){
    	var obj = global.jobReferenceMap.get(jobReferenceId);
    	
        $('#jobReferenceId').val(obj.id);
        $('#subject').val(obj.subject);
        $('#lightSoure').val(obj.lightSoure);
        $('#jobReferenceFilePath').val(obj.imagePath);
        $('#jobReferenceImage').attr("src",config.baseURL+obj.imagePath);
        
        $('#jobReferenceButton').val("Modify");
    },
    top:function(jobReferenceId){
        $.ajax({
            type: "POST",
            url: "../jobReference/top.json",
            cache:false,
            dataType: "json",
            data : {"id":jobReferenceId},
            success: function(data){
                if(data.result == "success"){
                    jobReference.list(currentJobReferencePageNo);
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
       });
    }
};